package com.shadow.baiqun.delegate.room.change;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.EditText;

import com.shadow.baiqun.R;
import com.shadow.baiqun.delegate.common.ResultConverter;
import com.shadow.core.net.callback.IError;
import com.shadow.core.net.callback.ISuccess;
import com.shadow.core.net.rx.RxRestClient;
import com.shadow.core.util.keyboard.KeyboardUtils;

import io.reactivex.disposables.Disposable;

/**
 * @author zhangyu
 * @date 2019/7/22
 */
public class DialogReason extends Dialog {
    private Disposable mDisposable;
    private OnPostDataListener mOnPostDataListener;

    public DialogReason(@NonNull final Context context, final String data,
                        OnPostDataListener onPostDataListener) {
        super(context);
        mOnPostDataListener = onPostDataListener;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).
                inflate(R.layout.dialog_reason, null);
        view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        final EditText etReason = view.findViewById(R.id.et_reason);
        view.findViewById(R.id.tv_commit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyboardUtils.toggleSoftInput(context);
                mDisposable = RxRestClient.builder()
                        .url("WMS/backstage/indoorChange/addChange")
                        .success(new ISuccess() {
                            @Override
                            public void onSuccess(String response) {
                                if (new ResultConverter().isSucceed(response)) {
                                    if (mOnPostDataListener != null) {
                                        mOnPostDataListener.onSucceed();
                                    }
                                } else {
                                    if (mOnPostDataListener != null) {
                                        mOnPostDataListener.onError();
                                    }
                                }
                                dismiss();
                            }
                        })
                        .error(new IError() {
                            @Override
                            public void onError(int code, String msg) {
                                if (mOnPostDataListener != null) {
                                    mOnPostDataListener.onError();
                                }
                                dismiss();
                            }
                        })
                        .params("list", data)
                        .params("note", etReason.getText().toString())
                        .build()
                        .unRestGet();
            }
        });
        setContentView(view);
    }

    @Override
    public void dismiss() {
        if (mDisposable != null) {
            mDisposable.dispose();
        }
        super.dismiss();
    }

    public interface OnPostDataListener {
        public void onSucceed();

        public void onError();
    }
}
